package com.phiture.erp.basic.web.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialInventoryPageReqDTO;
import com.phiture.erp.basic.core.pojo.dto.ErpMaterialInventorySaveReqDTO;
import com.phiture.erp.basic.core.pojo.vo.ErpMaterialInventoryRespVO;
import com.phiture.erp.basic.core.service.ErpMaterialInventoryService;
import com.phiture.erp.basic.dal.entity.ErpMaterialInventoryDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物料库存属性")
@RestController
@RequestMapping("/erp/material-inventory")
public class ErpMaterialInventoryController {

    @Resource
    private ErpMaterialInventoryService erpMaterialInventoryService;

    @PostMapping("/create")
    @Operation(summary = "创建物料库存属性")
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:create')")
    public CommonResult<Long> createMaterialInventory(@Valid @RequestBody ErpMaterialInventorySaveReqDTO createReqDTO) {
        return success(erpMaterialInventoryService.createMaterialInventory(createReqDTO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料库存属性")
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:update')")
    public CommonResult<Boolean> updateMaterialInventory(@Valid @RequestBody ErpMaterialInventorySaveReqDTO updateReqDTO) {
        erpMaterialInventoryService.updateMaterialInventory(updateReqDTO);
        return success(true);
    }

    @PutMapping("/createOrUpdate")
    @Operation(summary = "创建或者更新物料库存属性")
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:update')")
    public CommonResult<Boolean> createOrUpdateMaterialInventory(@Valid @RequestBody ErpMaterialInventorySaveReqDTO reqDTO) {
        erpMaterialInventoryService.crateOrUpdateMaterialInventory(reqDTO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料库存属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:delete')")
    public CommonResult<Boolean> deleteMaterialInventory(@RequestParam("id") Long id) {
        erpMaterialInventoryService.deleteMaterialInventory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料库存属性")
    @Parameter(name = "materialId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:query')")
    public CommonResult<ErpMaterialInventoryRespVO> getMaterialInventory(@RequestParam("materialId") Long materialId) {
        ErpMaterialInventoryDO materialInventory = erpMaterialInventoryService.getMaterialInventoryByMaterialId(materialId);
        return success(BeanUtils.toBean(materialInventory, ErpMaterialInventoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料库存属性分页")
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:query')")
    public CommonResult<PageResult<ErpMaterialInventoryRespVO>> getMaterialInventoryPage(@Valid ErpMaterialInventoryPageReqDTO pageReqDTO) {
        PageResult<ErpMaterialInventoryDO> pageResult = erpMaterialInventoryService.getMaterialInventoryPage(pageReqDTO);
        return success(BeanUtils.toBean(pageResult, ErpMaterialInventoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料库存属性 Excel")
    @PreAuthorize("@ss.hasPermission('erp:material-inventory:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialInventoryExcel(@Valid ErpMaterialInventoryPageReqDTO pageReqDTO, HttpServletResponse response) throws IOException {
        pageReqDTO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMaterialInventoryDO> list = erpMaterialInventoryService.getMaterialInventoryPage(pageReqDTO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料库存属性.xls", "数据", ErpMaterialInventoryRespVO.class,
                        BeanUtils.toBean(list, ErpMaterialInventoryRespVO.class));
    }

}